package com.yonyou.iuap.controller.pc;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yonyou.iuap.pub.DateUtil;
import com.yonyou.iuap.pub.JsonUtils;
import com.yonyou.iuap.shell.entity.BdProductEntity;
import com.yonyou.iuap.shell.pub.RequestDto;
import com.yonyou.iuap.shell.pub.ResultDto;
import com.yonyou.iuap.shell.service.intf.IKmPublishService;
import com.yonyou.iuap.shell.service.intf.IProductService;

@Controller
@RequestMapping(value="/shellProduct")
public class PCProductController {
	private final Logger logger = (Logger) LoggerFactory.getLogger(getClass());
	private long start,end;
	@Autowired
	private  IProductService iProductService;
	@Autowired
	private IKmPublishService kmPublishService;
	@SuppressWarnings("finally")
	@RequestMapping(value="/insert")
	@ResponseBody
	public String insert(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = new ResultDto();
		BdProductEntity product = new BdProductEntity();
		try {
			if(map.get("name")!=null){
				product.setProduct_name(map.get("name").toString());
			}
			product.setPk_creator(map.get("pk_user").toString());
			//是否启用
			String enable=map.get("enable").toString();
			product.setSealmark(enable);
			rsDto = iProductService.insert(product);
		} catch (NullPointerException e) {
			rsDto.setMessage("空指针,参数错误或缺少参数");
			rsDto.setStatus(1);
		}
		end=System.currentTimeMillis();
		logger.info("insert接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);

		
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public String update(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = new ResultDto();
		BdProductEntity product = new BdProductEntity();
		if(map.get("name")!=null){
			product.setProduct_name(map.get("name").toString());
		}
		//修改启用状态
		if(map.get("enable")!=null){
			product.setSealmark(map.get("enable").toString());
		}
		String pk_user = map.get("pk_user").toString();
		product.setPk_product(map.get("pk").toString());
		product.setUpdate_date(DateUtil.getDateTime());
		product.setUpdate_user(pk_user);
		rsDto = iProductService.updateByPrimaryKeySelective(product);
//		更新已发布知识关键字
		if(map.get("oldkey")!=null&&!product.getProduct_name().equals(map.get("oldkey").toString())){
			Map<String,String> parmap=new HashMap<String, String>();
			parmap.put("oldkey", map.get("oldkey").toString());
			parmap.put("newkey", product.getProduct_name());
			kmPublishService.updateKey(parmap);
		}
		end=System.currentTimeMillis();
		logger.info("update接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public String delete(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = new ResultDto();
		String pk = map.get("pk").toString();
		rsDto = iProductService.deleteByPrimaryKey(pk);
		end=System.currentTimeMillis();
		logger.info("delete接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);
	}
	@RequestMapping(value="/query")
	@ResponseBody
	public String query(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = new ResultDto();
		String name = map.get("name").toString();
		rsDto = iProductService.selectByCondition(" upper(product_name) like upper('%"+name+"%')");
		end=System.currentTimeMillis();
		logger.info("query接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);
	}
	@RequestMapping(value="/pageQuery")
	@ResponseBody
	public String pageQuery(@RequestBody Map map,HttpServletRequest request,HttpServletResponse response){
		start=System.currentTimeMillis();
		ResultDto rsDto = new ResultDto();
		String name = map.get("name").toString();
		RequestDto reqDto = new RequestDto();
		reqDto.setGroupKeys(map.get("groupkeys")==null?null:map.get("groupkeys").toString());
		reqDto.setOrderKey(map.get("orderkey")==null?null:map.get("orderkey").toString());
		reqDto.setOrderRule(map.get("orderrule")==null?"DESC":map.get("orderrule").toString());
		reqDto.setPageNum((Integer.valueOf(map.get("pageNum").toString())-1)*Integer.valueOf(map.get("pageSize").toString()));
		reqDto.setPageSize(Integer.valueOf(map.get("pageSize").toString()));
		if(!StringUtils.isEmpty(name)){
			reqDto.setParams(name);
		}
		
		rsDto = iProductService.pageQuery(reqDto);
		end=System.currentTimeMillis();
		logger.info("pageQuery接口调用时间"+String.valueOf(end-start));
		return JsonUtils.toJsonStr(rsDto);
	}
}
